#!/usr/brcm/ba/bin/perl
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
 
$ENV{'P4CONFIG'}="/home/usmankh/.p4config";
#my $log_file = "/projects/mcsi/scm_tools/P4Triggers/git-p4-log";
my $log_file = "/home/usmankh/git-p4-log";
#my ($p4client, $depot_path, $p4changelist, $p4port) = @ARGV;
my $depot_path = "//depot/Sources/SystemDevelopment/CP_Development/Releases/Usman_Test_CP_Development/msp/atc*";
chomp $depot_path;
my $bare_repo_path = 'ssh://mobcomgit@mobcom-git.sj.broadcom.com/git_repos/repo_msp/capri.git';
open  my $fileh, ">>$log_file" or die "Cannot open the logfile $!";
#$depot_path =~ s/(clib).*/$1/s;
$depot_path =~ s/(atc).*/$1/s;
my ($new_ws_name) = ($depot_path =~ m/Releases\/(.*?)\//);
my $ws_path = "/home/usmankh/project-capri/$new_ws_name";
 
update_git($depot_path, $ws_path, $new_ws_name, $bare_repo_path);
 
sub update_git {
  my ($new_p4_path, $path_to_ws, $wsname, $remote_repo) = @_;
  my $local_ws;
  my $p4_path;
  my $workspace = "/home/usmankh/testws";
  my $xml = XML::Simple->new;
  my $data_structure = $xml->XMLin("/home/usmankh/branch.xml");
  my $info = ($data_structure->{'branch'});
  foreach my $item (@$info) {
    $p4_path = $item->{'depot_path'};
    $local_ws = $item->{'git_ws'};
    if ($new_p4_path=~ /$p4_path/) {
      ws_exists($local_ws, $remote_repo);
    } else {
      create_ws($new_p4_path, $remote_repo, $ws_path);
      add_info($data_structure, $new_p4_path, $path_to_ws, $wsname);
      last;
    }
  }
}
sub ws_exists {
  my ($git_ws, $remote_repo_path) = @_;
  my ($cmd, $results);
  print $fileh "-------------------------------------------------------------------------------------------------------\n";
  print $fileh (localtime). "\n";
  print $fileh "Depo worksace already exists\n";
  print $fileh "CD into Workspace...\n";
  chdir $git_ws or die "Cannot CD into the $git_ws...$!";
  print $fileh "Rebasing the current Branch...\n";
  $cmd = "/tools/bin/git-p4 rebase 2>&1";
  $results = `$cmd`;
  print $fileh $results;
  print $fileh "pushing into remote bare repo...\n";
  print $fileh "git push $remote_repo_path\n";
  print $fileh (localtime). "\n";
  print $fileh "-------------------------------------------------------------------------------------------------------\n";
    }
sub create_ws {
  my ($cmd, $results);
  my ($new_depot_path, $bare_repo, $ws_name) = @_;
  print $fileh "-------------------------------------------------------------------------------------------------------\n";
  print $fileh (localtime). "\n";
  print $fileh "Depo workspace does not exist\n";
  print $fileh "Cloning a new git-p4 Depot workspace...\n";
  mkdir $ws_name or die "Unable to create $ws_name $!";
  print $fileh "CD into Workspace...\n";
  chdir $ws_name or die "Cannot CD into $ws_name... $!";
  $cmd = "/tools/bin/git-p4 clone $new_depot_path . 2>&1";
  $results = `$cmd`;
  print $fileh "$results";
  print $fileh "pushing into remote bare repo...\n";
  print $fileh "git push $bare_repo\n";
  print $fileh (localtime). "\n";
  print $fileh "--------------------------------------------------------------------------------------------------------\n";
  }
sub add_info {
  my $file = "/home/usmankh/branch.xml";
  my ($hash_structure, $dpath, $git_ws_path, $branch_name) = @_;
  my $xml = XML::Simple->new;
 
  my $array_structure = ($hash_structure->{'branch'});
  push @{$hash_structure->{'branch'}}, {
  depot_path  => $dpath,
  git_ws      => $git_ws_path,
  branch_name => $branch_name
  };
 
  my $output = $xml->XMLout($hash_structure, Rootname=>'branch_specs', XMLDecl => 1);
  open (my $file_handle, '>', $file) or die "Cannot open this file $!";
  print $file_handle "$output\n";
  close ($file_handle);
  return;
} #add_info
